#include "stdio.h"
 // #include <cstdio>
#include <string.h>
#include "LocalSdk.h"
#include "stdlib.h"
#include "inifiles.h"



#include "signal.h"
#include "netdb.h"
#include <arpa/inet.h>
#include <sys/wait.h>
#include <errno.h>

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <linux/sockios.h>

#include "xmcamera.h"

#define kVersionString "0.1"
#define kBuildString "15.0813"

int get_mac(char* mac, const char* ethDeviceName)
{
    struct ifreq tmp;
    int sock_mac;
    char mac_addr[30];
    sock_mac = socket(AF_INET, SOCK_STREAM, 0);
    if( sock_mac == -1){
        perror("create socket fail\n");
        return -1;
    }
    memset(&tmp,0,sizeof(tmp));
    strncpy(tmp.ifr_name,ethDeviceName,sizeof(tmp.ifr_name)-1 );
    if( (ioctl( sock_mac, SIOCGIFHWADDR, &tmp)) < 0 ){
        printf("mac ioctl error\n");
        return -1;
    }
    sprintf(mac_addr, "%02x%02x%02x%02x%02x%02x",
            (unsigned char)tmp.ifr_hwaddr.sa_data[0],
            (unsigned char)tmp.ifr_hwaddr.sa_data[1],
            (unsigned char)tmp.ifr_hwaddr.sa_data[2],
            (unsigned char)tmp.ifr_hwaddr.sa_data[3],
            (unsigned char)tmp.ifr_hwaddr.sa_data[4],
            (unsigned char)tmp.ifr_hwaddr.sa_data[5]
            );
    printf("local mac:%s\n", mac_addr);
    close(sock_mac);
    memcpy(mac,mac_addr,strlen(mac_addr)+1);
    return 0;
}
int run()
{
    //Éè±¸µØÖ·
    string sDeviceAddr = IniReadString("device", "address", "127.0.0.1", "/mnt/custom/xmcamera.ini");
    //Éè±¸¶Ë¿Ú
    int iDevicePort = IniReadInteger("device", "port", 80, "/mnt/custom/xmcamera.ini");
    //Éè±¸Íø¿¨Ãû³Æ 
    string sDeviceNetInterface = IniReadString("device", "net_interface", "eth0", "/mnt/custom/xmcamera.ini");
    //Éè±¸ÓÃ»§Ãû
    string sDeviceUserName = IniReadString("device", "username", "admin", "/mnt/custom/xmcamera.ini");
    //Éè±¸ÃÜÂë
    string sDevicePassword = IniReadString("device", "password", "admin", "/mnt/custom/xmcamera.ini");
    //Éè±¸ÂëÁ÷Êä³ö£¬1£ºÖ÷ÂëÁ÷ 0£º´ÎÂëÁ÷
    int iStreamFlag = IniReadInteger("device", "stream_flag", 1, "/mnt/custom/xmcamera.ini");
    //ÊÇ·ñÊä³öÒôÆµ£¬1£ºÊä³ö 0£º²»Êä³ö
    int iAudioEnable = IniReadInteger("device", "audio_enable", 1, "/mnt/custom/xmcamera.ini");
    //ÒôÆµÊÇ·ñAAC±àÂë£¬1£º±àÂë 0£º²»±àÂë
    int iAACEncode = IniReadInteger("device", "aac_encode", 1, "/mnt/custom/xmcamera.ini");
    //Éè±¸¶Ë»º³å´óÐ¡£¬0~500£¬0±íÊ¾²»ÏÞÖÆ
    int iMediaBufferSize = IniReadInteger("device", "media_buf_size", 100, "/mnt/custom/xmcamera.ini");
    //SMSµØÖ·
    string sSMSAddr = IniReadString("server", "sms_addr", "192.168.1.168", "/mnt/custom/xmcamera.ini");
    //SMS¶Ë¿Ú
    int iSMSPort = IniReadInteger("server", "sms_port", 554, "/mnt/custom/xmcamera.ini");
    //Audio ²ÉÑùÂÊ 
    int iAudio_sample_rate=IniReadInteger("device","audio_sample_rate",8000,"/mnt/custom/xmcamera.ini");
    
    string sDeviceSerial;
    char mac[30];
    if(get_mac(mac,sDeviceNetInterface.c_str())==0)
    	sDeviceSerial = string(mac);
    else
        {
        	printf("Wring:Could not obtain MAC address on interface-%s",sDeviceNetInterface.c_str());
        	return -1;
		}
    	
    
    printf("device[%s]/v%s (Build/%s) is now running\n", sDeviceSerial.c_str(), kVersionString, kBuildString);
    return 0;
}
int main()
{
	run();
	return 0;
}